package someTestExcemple.huaweiOd.dp;
//华为OD机试 - 最佳的出牌方法 - 深度优先搜索DFS、动态规划（Java 2025 A卷 200分）
//https://blog.csdn.net/guorui_java/article/details/147021589
import java.util.*;
public class TheBestWayTopPlayACard {
    private static int maxScore = -1;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        sc.close();
        int[] cards = new int[14]; //0号位不用 1-13分别表示A-K的牌的数量
        int cardNum =0;
        for(char c: input.toCharArray()) {
            cardNum++;
            if(c >='1' && c<='9') {
                cards[c-'0']++;
            } else if(c=='0') {
                cards[10]++;
            } else if(c=='J') {
                cards[11]++;
            } else if(c=='Q') {
                cards[12]++;
            } else if(c=='K') {
                cards[13]++;
            }
        }
        boolean[] empty = new boolean[13];
        for(int i=1;i<=13;i++) {
            if(cards[i]==0) {
                empty[i-1] = true;
            }
        }
        int sum=0;
        int emptyNum =0;
        dfs(cards,empty,sum,cardNum);
        System.out.println(maxScore);

    }

    private static void dfs(int[] cards,boolean[] empty,int sum,int cardNum) {
        while(true) {
            if(cardNum ==0) {
                maxScore = Math.max(maxScore,sum);
                return; //递归终止条件
            }
            for(int i=13;i>=1;i--) {
                if(empty[i-1]) {
                    continue; //已经空了跳过
                }
                if(cards[i]==0 && !empty[i-1]) {
                    empty[i-1] = true;
                    cardNum--;
                } else if(cards[i] >= 4) {
                    //尝试出炸弹
                    cards[i]-=4;
                    sum+= i*4*3;
                    dfs(cards,empty,sum,cardNum);
                    //回溯
                    sum-= i*4*3;
                    cards[i]+=4;
                } else {
                    int nextFive = haveNextFive(cards);
                    if(nextFive != -1) {
                        //出顺子
                        sum+= (nextFive + nextFive+1 + nextFive+2 + nextFive+3
                                + nextFive+4)*2;
                        cards[nextFive]--;
                        cards[nextFive+1]--;
                        cards[nextFive+2]--;
                        cards[nextFive+3]--;
                        cards[nextFive+4]--;
                        dfs(cards,empty,sum,cardNum);

                        //回溯
                        sum-= (nextFive + nextFive+1 + nextFive+2 + nextFive+3
                                + nextFive+4)*2;
                        cards[nextFive]++;
                        cards[nextFive+1]++;
                        cards[nextFive+2]++;
                        cards[nextFive+3]++;
                        cards[nextFive+4]++;

                    } else {
                        if(cards[i] >=3) {
                            //出3张
                            cards[i] -=3;
                            sum += i*3*2;
                            dfs(cards,empty,sum,cardNum);
                            //回溯
                            cards[i]+=3;
                            sum-=i*3*2;
                        } else if(cards[i] >=2) {
                            //出2张
                            cards[i] -=2;
                            sum += i*2*2;
                            dfs(cards,empty,sum,cardNum);
                            //回溯
                            cards[i]+=2;
                            sum-=i*2*2;
                        } else if(cards[i] == 1) {
                            cards[i]--;
                            sum+=i;
                            dfs(cards,empty,sum,cardNum);
                            //回溯
                            cards[i]++;
                            sum-=i;
                        }

                    }
                }
            }
        }
    }

    private static int haveNextFive(int[] cards) {
        int result =-1;
        for(int i=9;i>=1;i--) {
            if(cards[i] >=1 && cards[i+1] >=1 && cards[i+2] >=1 && cards[i+3] >=1
                && cards[i+4] >=1) {
                result =i;
                break;
            }
        }
        return result;
    }
}
